﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ZoneMediaManager
{
    public ZoneMediaManager()
    {
        LoadAllUnit();
    }

    public List<nUnitData> _listnUnitData = new List<nUnitData>();

    public void LoadAllUnit()
    {
        _listnUnitData.Clear();

        string[] files = System.IO.Directory.GetFiles(ZoneConfig.CsvPath, "*.txt");

        for (int i = 0; i < files.Length; ++i)
        {
            string content = System.IO.File.ReadAllText(files[i]);

            nUnitData data = JsonUtility.FromJson<nUnitData>(content);

            _listnUnitData.Add(data);
        }
    }

    public nUnitData[] GetAllRes(nUnitType type)
    {
        List<nUnitData> files = new List<nUnitData>();

        for (int i = 0; i < _listnUnitData.Count; ++i)
        {
            if (_listnUnitData[i]._UnitType != type)
                continue;

            files.Add(_listnUnitData[i]);
        }

        return files.ToArray();
    }

    public nUnitData GetDataByID(int id)
    {
        for (int i = 0; i < _listnUnitData.Count; ++i)
        {
            if (_listnUnitData[i].id == id)
            {
                return _listnUnitData[i];
            }

        }

        return null;
    }

    public string GetPathRes(nUnitType type, string name)
    {
        string filepath = GetResDir(type) + name;

        return filepath;
    }

    /// <summary>
    /// 获取一个
    /// </summary>
    /// <param name="type"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public string GetResAssetPathWithOutExtension(nUnitType type, string name)
    {
        string filepath = GetResDir(type) + name;

        int resIndex = filepath.IndexOf("Resources/");

        string assetfilePath = filepath.Substring(resIndex + 10);

        return assetfilePath;
    }

    public string GetResDir(nUnitType type)
    {
        string path = Application.dataPath + "/Game/Resources/media2d/";

        switch (type)
        {
            case nUnitType.Building:
                path += "floor/";
                break;
            case nUnitType.Decoration:
                path += "deco/";
                break;
            case nUnitType.Wall:
                path += "wall/";
                break;
            case nUnitType.Foundation:
                path += "foundation/";
                break;
            case nUnitType.Role:
            case nUnitType.Null:
                break;
        }

        return path;
    }

    /// <summary>
    /// 获取Resource实例对象
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public T GetResource2DObject<T>(nUnitData data) where T : UnityEngine.Object
    {
        string name = data._res_name;

        T o = AssetManager.Instance.GetResourceObject<T>(name);

        if (o == null)
        {
            Debug.LogError("GetResource2DObject: " + data._res_name);
        }

        return o;
    }

    public T GetResource3DObject<T>(nUnitData data) where T : UnityEngine.Object
    {
        T o = AssetManager.Instance.GetResourceObject<T>(data.spine_name);

        if (o == null)
        {
            Debug.LogError("GetResource2DObject: " + data.spine_name);
        }

        return o;
    }

}
